<?php
/**
 * @Created on 2021/05/25
 * @Author Qi
*/
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Consume extends Base{

    //消费明细
    public function detail(){

        $where = array();
        $where['type'] = 1;
        $account=request()->param('account');
        if($account){
            $where['account'] = array('like',"%{$account}%");
        }
        $a_id=request()->param('a_id');
        if($a_id){
            $where['a_id'] = array('like',"%{$a_id}%");
        }
        $ader=request()->param('ader');
        if($ader){
            $where['ader_id'] = $ader;
        }
        $m_id=request()->param('m_id');
        if($m_id){
            $where['m_id'] = $m_id;
        }
        $start=request()->param('start');
        if($start){
            $where['e_date'] = array('egt',$start);
        }
        $end=request()->param('end');
        if($end){
            $where['e_date'] = array('elt',$end);
        }
        if(!empty($start) && !empty($end)){
            $where['e_date'] = array('between time',["{$start}","{$end}"]);
        }
        //筛选下拉
        $adlist=db('ader')->order('id desc')->select();
        $memlist=db('member')->field('uid,username')->where('roleid',2)->order('uid desc')->select();
        $list=db('account_excel')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        //总消费币
        $zxfb = db('account_excel')->where($where)->sum('day_consum');
        //消费金额
        $xfje = db('account_excel')->where($where)->sum('daymoney');
        //总利润
        $zlr = db('account_excel')->where($where)->sum('dayprofit');
        $this->assign('list',$list);
        $this->assign('account',$account);
        $this->assign('a_id',$a_id);
        $this->assign('ader',$ader);
        $this->assign('m_id',$m_id);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('adlist',$adlist);
        $this->assign('memlist',$memlist);
        $this->assign('zxfb',$zxfb);
        $this->assign('xfje',$xfje);
        $this->assign('zlr',$zlr);
        return $this->fetch();
    }

    //客户消费报表
    public function custlist(){

        $where = array();
        $where['type'] = 1;
        $account=request()->param('account');
        if($account){
            $where['account'] = array('like',"%{$account}%");
        }
        $a_id=request()->param('a_id');
        if($a_id){
            $where['a_id'] = array('like',"%{$a_id}%");
        }
        $m_id=request()->param('m_id');
        if($m_id){
            $where['m_id'] = $m_id;
        }
        $start=request()->param('start');
        if($start){
            $where['e_date'] = array('egt',$start);
        }
        $end=request()->param('end');
        if($end){
            $where['e_date'] = array('elt',$end);
        }
        if(!empty($start) && !empty($end)){
            $where['e_date'] = array('between time',["{$start}","{$end}"]);
        }
        //筛选下拉
        $adlist=db('ader')->order('id desc')->select();
        $memlist=db('member')->field('uid,username')->where('roleid',2)->order('uid desc')->select();
        $list=db('account_excel')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        //消费币金额
        $zxfb = db('account_excel')->where($where)->sum('day_consum');
        //总利润
        $zlr = db('account_excel')->where($where)->sum('dayprofit');
        $this->assign('list',$list);
        $this->assign('account',$account);
        $this->assign('a_id',$a_id);
        $this->assign('m_id',$m_id);
        $this->assign('start',$start);
        $this->assign('end',$end);
        $this->assign('adlist',$adlist);
        $this->assign('memlist',$memlist);
        $this->assign('zxfb',$zxfb);
        $this->assign('zlr',$zlr);
        return $this->fetch();
    }

    //客户账户明细
    public function custdet(){

        $where = array();
        $type=request()->param('type');
        if($type){
            $where['type'] = $type;
        }
        $start=request()->param('start');
        if($start){
            $where['e_date'] = array('egt',$start);
        }
        $end=request()->param('end');
        if($end){
            $where['e_date'] = array('elt',$end);
        }
        if(!empty($start) && !empty($end)){
            $where['e_date'] = array('between time',["{$start}","{$end}"]);
        }
        $list=db('customer_detail')->where($where)->order('id desc')->paginate(50,false,['query'=>request()->param()]);
        $this->assign('list',$list);
        $this->assign('type',$type);
        $this->assign('start',$start);
        $this->assign('end',$end);
        return $this->fetch();
    }

    //添加客户充值退款记录
    public function addrecharge(){

        if(request()->isPost()){
            $cus_id = trim(request()->param('cus_id'));
            $cus_type = trim(request()->param('cus_type'));
            $type = trim(request()->param('type'));
            $money = trim(request()->param('money'));
            $e_date = trim(request()->param('e_date'));
            $compayacc = trim(request()->param('compayacc'));
            $remarks = trim(request()->param('remarks'));
            $data['cus_id'] = $cus_id;
            $data['cus_name'] = db('customer')->where(['id'=>$cus_id])->value('name');
            $data['cus_type'] = $cus_type;
            $data['type'] = $type;
            $data['money'] = $money;
            $data['e_date'] = $e_date;
            $data['compayacc'] = $compayacc;
            $data['remarks'] = $remarks;
            $data['add_time'] = time();
            $data['do_user'] = session('username');
            $data['do_id'] = session('uid');
            $true=db('customer_detail')->insert($data);
            if($true){
                //将报表明细中充值字段同步
                // if($type == 2){
                //     $accids = db('project')->field('accid')->where('cus_id',$cus_id)->select();
                //     if($accids){
                //         foreach ($accids as $k => $v) {
                //             db('account_excel')->where(['e_date'=>$e_date,'account_id'=>$v['accid']])->update(['recharge'=>$money]);
                //         }
                //     }
                // }
                system_log('customer_detail','添加客户充值退款记录');
                return json(['status'=>1,'message'=>'添加成功']);
            }else{
                return json(['status'=>0,'message'=>'添加失败']);
            }
        }else{
            $cuslist=db('customer')->field('id,name')->order('id desc')->select();
            $this->assign('cuslist',$cuslist);
            return $this->fetch();
        }
    }

    //修改减款及服务费
    public function editreduce(){
        if(request()->isPost()){
            $id = request()->param('id');
            $reduce = trim(request()->param('reduce'));
            $service = trim(request()->param('service'));
            $data['reduce'] = $reduce;
            $data['service'] = $service;
            $true=db('account_excel')->where('id',$id)->update($data);
            if($true){
                system_log('account_excel','修改客户减款及服务费');
                return json(['status'=>1,'message'=>'修改成功']);
            }else{
                return json(['status'=>0,'message'=>'修改失败']);
            }
        }else{
            $id=request()->param('id');
            $info=db('account_excel')->where('id',$id)->find();
            $this->assign('info',$info);
            return $this->fetch();
        }
    }

    //删除
    public function del(){
        $id=request()->param('id');
        $true=db('account_excel')->where('id',$id)->delete();
        if($true){
            system_log('account_excel','删除报表数据');
            return json(['status'=>1,'message'=>'删除成功']);
        }else{
            return json(['status'=>0,'message'=>'删除失败']);
        }
    }

}